<template>
  <div class="foodscontrast">
    <!-- 食物对比页面 -->
    <van-sticky>
      <div class="foodscontrasttop">
        <span class="brank" @click="gobackhome"></span>
        <span class="title">对比详情</span>
      </div>
      <div class="tishi" v-if="flag">
        <span>还需比较请再次点击图片</span>
      </div>

      <div class="foodscontrastconter">
        <div class="onetitle" @click="onekongbai">
          <img
            src="../assets/image/img_compare_add.png"
            v-if="!onekongbailist"
          />
          <van-image
            v-else
            width="80"
            height="80"
            :src="onekongbailist.thumb_image_url"
          />
          <div v-if="onekongbailist" class="title">
            {{ onekongbailist.name }}
          </div>
        </div>
        <div class="titlevs"><span>VS</span></div>
        <div class="twotitle" @click="twokongbai">
          <img
            src="../assets/image/img_compare_add.png"
            v-if="!twokongbailist"
          />
          <van-image
            v-else
            width="80"
            height="80"
            :src="twokongbailist.thumb_image_url"
          />
          <div v-if="twokongbailist" class="title">
            {{ twokongbailist.name }}
          </div>
        </div>
      </div>
    </van-sticky>
    <div class="nourishment" v-if="onekongbailist || onekongbailist">
      <table>
        <thead>
          <tr>
            <th colspan="4">营养元素</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="item in zhonghe" :key="item.id">
            <td>
              <span v-if="item.Tone">{{ item.Tone }}{{ item.danwei }}</span
              ><span v-else>--</span>
            </td>
            <td>{{ item.title }}</td>
            <td>
              <span v-if="item.Ttwo">{{ item.Ttwo }}{{ item.danwei }}</span>
              <span v-else>--</span>
            </td>
          </tr>
        </tbody>
      </table>
      <!-- 第一个数据 -->
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";

export default {
  data() {
    return {
      list: [
        {
          id: "calory",
          title: "热量",
          danwei: "千卡",
        },
        {
          id: "protein",
          title: "蛋白质",
          danwei: "克",
        },
        {
          id: "fat",
          title: "脂肪",
          danwei: "克",
        },
        {
          id: "carbohydrate",
          title: "碳水化合物",
          danwei: "克",
        },
        {
          id: "fiber_dietary",
          title: "膳食纤维",
          danwei: "克",
        },

        {
          id: "vitamin_a",
          title: "维生素A",
          danwei: "ugRE",
        },
        {
          id: "vitamin_c",
          title: "维生素C",
          danwei: "毫克",
        },
        {
          id: "vitamin_e",
          title: "维生素E",
          danwei: "毫克",
        },
        {
          id: "carotene",
          title: "胡萝卜素",
          danwei: "微克",
        },
        {
          id: "thiamine",
          title: "维生素B1",
          danwei: "毫克",
        },
        {
          id: "lactoflavin",
          title: "维生素B2",
          danwei: "毫克",
        },
        {
          id: "niacin",
          title: "烟酸",
          danwei: "毫克",
        },
        {
          id: "cholesterol",
          title: "胆固醇",
          danwei: "毫克",
        },
        {
          id: "magnesium",
          title: "镁",
          danwei: "毫克",
        },
        {
          id: "calcium",
          title: "钙",
          danwei: "毫克",
        },
        {
          id: "iron",
          title: "铁",
          danwei: "毫克",
        },
        {
          id: "zinc",
          title: "锌",
          danwei: "毫克",
        },
        {
          id: "copper",
          title: "铜",
          danwei: "毫克",
        },
        {
          id: "manganese",
          title: "猛",
          danwei: "毫克",
        },
        {
          id: "kalium",
          title: "钾",
          danwei: "毫克",
        },
        {
          id: "phosphor",
          title: "磷",
          danwei: "毫克",
        },
        {
          id: "natrium",
          title: "钠",
          danwei: "毫克",
        },
        {
          id: "selenium",
          title: "晒",
          danwei: "毫克",
        },
      ],
      newlist: [],
      flag: false,
    };
  },
  computed: {
    ...mapState(["onekongbailist", "twokongbailist", "zhonghe"]),
    lastlist() {
      if (this.onekongbailist !== null) {
        return this.list.map((item) => {
          return {
            ...item,
            Tone: this.onekongbailist.ingredient[item.id],
          };
        });
      } else if (this.twokongbailist !== null) {
        return this.list.map((item) => {
          return {
            ...item,
            Ttwo: this.twokongbailist.ingredient[item.id],
          };
        });
      } else {
        return [];
      }
    },
  },
  created() {},

  methods: {
    ...mapMutations(["setonekongbailist", "settwokongbailist", "setzhonghe"]),
    onekongbai() {
      this.$router.push({ path: "/search" });
      window.localStorage.setItem("number", 1);
    },
    twokongbai() {
      this.$router.push({ path: "/search" });
      window.localStorage.setItem("number", 2);
    },
    gobackhome() {
      //跳到首页清空数据
      this.$router.push({ path: "/" });
      this.setonekongbailist(null);
      this.settwokongbailist(null);
    },
  },
  watch: {
    onekongbailist(n) {
      if (this.twokongbailist == null) {
        this.newlist = this.list.map((item) => {
          return {
            ...item,
            Tone: n.ingredient[item.id],
          };
        });
      } else {
        this.newlist = this.list.map((item) => {
          return {
            ...item,
            Tone: n.ingredient[item.id],
            Ttwo: this.twokongbailist.ingredient[item.id],
          };
        });
        this.flag = true;
        setTimeout(() => {
          this.flag = false;
        }, 1500);
      }
      this.setzhonghe(this.newlist);
    },
    twokongbailist(n) {
      if (this.onekongbailist == null) {
        this.newlist = this.list.map((item) => {
          return {
            ...item,
            Ttwo: n.ingredient[item.id],
          };
        });
      } else {
        this.newlist = this.list.map((item) => {
          return {
            ...item,
            Tone: this.onekongbailist.ingredient[item.id],
            Ttwo: n.ingredient[item.id],
          };
        });
        this.flag = true;
        setTimeout(() => {
          this.flag = false;
        }, 1500);
      }
      this.setzhonghe(this.newlist);
    },
  },
};
</script>

<style lang="less" scoped>
.foodscontrast {
  width: 100vw;
  position: relative;
  .foodscontrasttop {
    width: 100%;
    display: flex;
    align-items: center;
    border-bottom: 1px solid gray;
    padding: 5px 10px;
    background: white;
    .brank {
      display: block;
      width: 40px;
      height: 40px;
      background: url("../assets/image/ic_back_dark_center.png") no-repeat;
      background-size: cover;
      background-position: center;
    }
    .title {
      margin-left: 20px;
      font-size: 18px;
    }
  }
  .tishi {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    padding: 5px 5px;
    background: rgba(0, 0, 0, 0.5);
    color: white;
    border-radius: 15px;
  }
  .foodscontrastconter {
    width: 100%;
    display: flex;
    justify-content: center;
    background: #f6f6f6;
    padding: 30px;
    align-items: flex-end;

    div {
      position: relative;
      .title {
        font-size: 15px;
        width: 10em;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        text-align: center;
        // border: 1px solid red;
        padding: 5px 0;
      }
      img {
        width: 100%;
      }
    }
    .onetitle {
      position: relative;
      width: 90px;
      .titleimg {
        position: absolute;
        width: 30px;
        top: -15px;
        right: -4px;
        img {
          width: 100%;
        }
      }
      .title {
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
      }
    }
    .titlevs {
      width: 90px;
      height: 90px;
      // border: 1px solid red;
      display: flex;
      justify-content: center;
      align-items: flex-end;
      padding-bottom: 10px;
      span {
        font-size: 20px;
        color: red;
        padding: 10px 9px;
        border: 1px solid red;
        border-radius: 50%;
      }
    }
    .twotitle {
      width: 90px;
      .titleimg {
        position: absolute;
        width: 30px;
        top: -15px;
        right: -4px;
        img {
          width: 100%;
        }
      }
      .title {
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
      }
    }
  }
  // border-collapse: collapse; /* IE7 and lower */ border-spacing: 0;
  .nourishment {
    width: 100%;
    padding: 10px 10px;
    table {
      width: 100%;
      border-collapse: collapse;
      th {
        // width: 33%;
        text-align: center;
      }
      tbody {
        tr {
          border-bottom: 1px solid rgba(128, 128, 128, 0.493);
          &:last-child {
            border-bottom: 0;
          }
          td {
            width: 33%;
            padding: 8px 0;
            font-size: 15px;
            &:nth-child(1) {
              text-align: left;
            }
            &:nth-child(2) {
              text-align: center;
            }
            &:nth-child(3) {
              text-align: right;
            }
          }
        }
      }
      // td {
      //   width: 33%;
      // }
    }
    // table {
    //   border-collapse: collapse;
    //   border-spacing: 1px;
    // }
  }
}
</style>